require('es6-promise').polyfill(); //重构promise方法，解决浏览器兼容问题

import Vue from 'vue'
import App from './app'
import axios from 'axios'
import VueRouter from 'vue-router'
import Routers from './routers'
//import Modal from 'iview/src/components/modal';
import iView from 'iview';
import Loading from 'toon-ui/lib/components/loading'
import 'iview/dist/styles/iview.css';
import './pc-common.css';    // 重写iview css
import './iview-reset.css';    // 重写iview css


import util from 'assets/js/util'
import Toast from 'toon-ui/lib/components/toast'
import appConfig from 'assets/js/app.config'

Vue.use(iView);

const  HOST = configM.comUrl ;
const  TOON_CODE = util.getQueryStringHr('userToken')  || configM.code || "" ;

// 全局配置
Vue.config.debug = false
Vue.config.devtools = false
Vue.config.silent = true
Vue.prototype.$util = util
var axiosInstance = axios.create()
Vue.prototype.$http = axiosInstance

//========创建vue实例==========
let  initVue = ()=>{
  // 并且配置路由规则
  Vue.use(VueRouter)
  const router = new VueRouter({
    mode: 'hash',
    routes: Routers
  })
  router.beforeEach((to, from, next) => {
    if( !navigator.onLine ){
      Toast({
        message: '无网络链接',
        duration: 1000, // 持续时间，默认3000毫秒
      });
      return ;
    }
    next() ;
  })
  new Vue({
    router,
    render: h => h(App)
  }).$mount('#app')

}
/**
 * 自定义配置
 * @type {Object|*}
 */
//========axiosInstance==========
axiosInstance.defaults.baseURL =HOST;
axiosInstance.defaults.timeout = 30000;
axiosInstance.defaults.loading = true;
axiosInstance.defaults.sessionId = true;
axiosInstance.defaults.headers.post['content-type'] = 'application/json;charset=utf-8';
var SpecialCharacter= function(sourceData) {
  var result=sourceData;
  for (var key in sourceData) {
    if(typeof sourceData[key]==='object'||typeof sourceData[key]==='array'){
      SpecialCharacter(sourceData[key])
    }else if(typeof sourceData[key]==='string'  ){
      result[key]=  sourceData[key].replace(/\"/g,"＂").replace(/\</g,'《').replace(/\>/g,'》').replace(/\\/g,'＼').replace(/\'/g,'’').replace(/\&/g,'＆').replace(/\#/g,'＃') ;
    }
  }
  return result;
}
// Add a request interceptor请求拦截
axiosInstance.interceptors.request.use(function (config) {
  config.data = SpecialCharacter(config.data)
  if(config.loading) {
    Loading.open('');
  }
  if(config.sessionId) {
    config.headers.sessionId = util.sessionId();
  }
  return config;
}, function (error) {
  Loading.close('')
  return Promise.reject(error);
});
// Add a response interceptor请求响应拦截
axiosInstance.interceptors.response.use(function (response) {
  Loading.close('')
  var code=response.data&&response.data.meta.code
  if(code==appConfig.respCode.SESSION_TIMEOUT||code==appConfig.respCode.TOONCODE_TIMEOUT||code==appConfig.respCode.TOKEN_TIMEOUT){
    Toast({
      message: '操作超时',
      duration: 3000 // 持续时间，默认3000毫秒
    });
    sessionStorage.removeItem('sessionId');
    return Promise.reject(response.data)
  }else{
    return response.data;
  }
}, function (error) {
  Loading.close('')
  if(error.response){
  }else{
    console.log('Error: ',error.message);
  }
  return Promise.reject(error);
});

//========axiosInstance==========
/**
 * 授权getSessionIdByTOON_CODE
 */
// if(util.getCookie("got_token")) {
//   initVue();
// }else{
  let api = "session/getSessionByUserToken?userToken="+TOON_CODE;
    //param =  {userToken:TOON_CODE};
  /*
    axiosInstance.post(HOST+ api , param  )
      .then(function (res) {
        //code解密失败,下载新的北京通
        if(res.meta.code == "0" ){
          util.sessionId(res.data.sessionId);
          initVue();
        }else{
          Toast({
            message: res.meta.message,
            duration: 1000, // 持续时间，默认3000毫秒
          });
        }
      })
      .catch(function (err) {
        console.log(err);
      });*/
  //}
   axiosInstance.get(HOST+ api)
     .then(function (res) {
       //code解密失败,下载新的北京通
       if(res.meta.code == "0" && res.data && res.data.sessionId){
         util.sessionId(res.data.sessionId);
         initVue();
       }else{
         Toast({
           message: '获取会话信息失败',
           duration: 1000 // 持续时间，默认3000毫秒
         });
       }
     })
     .catch(function (err) {
       console.log(err);
     });
// }

















